﻿// Sequin.WebLogs.IO.LogReader
//
// (c) 2011 Arthur Pitman
//
// Part of the Sequin Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License (Version 2.1) as published by the Free Software Foundation.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License (Version 2.1) along with this library; if not, write to the 
// Free Software Foundation, Inc., 
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
// or see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Sequin.WebLogs.IO
{
    /// <summary>
    /// Abstract base class for other log readers
    /// </summary>
    public abstract class LogReader : ILogReader
    {
        /// <summary>
        /// Queue of filenames to read
        /// </summary> 
        protected Queue<string> sourceFilenames = new Queue<string>();

        /// <summary>
        /// The current <see cref="StreamReader"/> 
        /// </summary> 
        protected StreamReader reader = null;

        /// <summary>
        /// Initializes a new <see cref="LogReader"/> instance with file or files specified by path parameter
        /// </summary>
        /// <param name="path">The location of file or files</param>
        public LogReader(string path)
        {          
            // enqueue single file or all files in a directory
            if (File.Exists(path))
            {
                sourceFilenames.Enqueue(path);
            }
            else if (Directory.Exists(path))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                foreach (var file in directoryInfo.GetFiles().OrderBy(a => a.Name))
                    sourceFilenames.Enqueue(file.FullName);
            }
            else
            {
                throw new ArgumentException("Unable to determine if path is a file or directory");
            }
        }

        /// <summary>
        /// Abstract method to read a <see cref="LogEntry"/> and satisfy the <see cref="ILogReader"/> interface
        /// </summary>
        /// <returns>The <see cref="LogEntry"/> or null if none is available</returns>
        public abstract LogEntry ReadLogEntry();

        /// <summary>
        /// Reads the next line
        /// </summary>
        /// <returns>The line or null if none is available</returns>
        protected string ReadLine()
        {
            while (true)
            {
                // if no file is open, try to open one
                while (reader == null)
                {
                    if (sourceFilenames.Count == 0)
                        return null;
                    reader = File.OpenText(sourceFilenames.Dequeue());
                }

                // read a line
                string line = reader.ReadLine();
                if (line != null)
                    return line;
                else
                    Close();
            }
        }

        /// <summary>
        /// Closes the open <see cref="StreamReader"/>
        /// </summary>
        public void Close()
        {
            if (reader != null)
            {
                reader.Close();
                reader = null;
            }
        }
    }
}
